﻿<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UC_BaiduMap.ascx.cs" Inherits="ZLCMS.Web.UC.UC_BaiduMap" %>

<div>
<%if(!ReadOnly) {%> <div style="line-height:30px;">
<input type="text" id="search" style="width:200px;" placeholder='请输入关键词'/><input type="button" value="搜索" onclick="baidu_localSearch.search(document.getElementById('search').value);"/><br/>
</div>
<%} %>
<div style="display:block">
<div style="width:400px;height:300px;border:1px solid gray" id="baidumap_container" runat="server"></div>
</div>
    
<asp:TextBox ID="TextBoxJingDu" runat="server" style="display:none">113.330438</asp:TextBox>
<asp:TextBox ID="TextBoxWeiDu" runat="server" style="display:none">23.139656</asp:TextBox>

</div>
<script type="text/javascript">
    var map = new BMap.Map("<%= baidumap_container.ClientID %>"); // 创建Map实例
    
    var point = new BMap.Point(document.getElementById("<%=TextBoxJingDu.ClientID %>").value,
        document.getElementById("<%=TextBoxWeiDu.ClientID %>").value); // 创建点坐枿
        
    map.centerAndZoom(point, 15); // 初始化地图，设置中心点坐标和地图级别⾿
    map.enableScrollWheelZoom(); //启用滚轮放大缩小

    var marker = new BMap.Marker(point); // 创建标注 
    map.addOverlay(marker);
    <%if(!ReadOnly) {%>   
    marker.enableDragging(true);
    marker.addEventListener("dragend", function (e) {
        document.getElementById("<%=TextBoxJingDu.ClientID %>").value = e.point.lng;
        document.getElementById("<%=TextBoxWeiDu.ClientID %>").value = e.point.lat;
        map.centerAndZoom(e.point, map.getZoom());
    });
    <%} %>
    
    <%if(!ReadOnly) {%>    
    map.addEventListener("click", function(e) {
        marker.setPosition(e.point);
        document.getElementById("<%=TextBoxJingDu.ClientID %>").value = e.point.lng;
        document.getElementById("<%=TextBoxWeiDu.ClientID %>").value = e.point.lat;
        map.centerAndZoom(e.point, map.getZoom());
    });
    var baidu_localSearch = new BMap.LocalSearch(map, {
      onSearchComplete:function(results){
        if (baidu_localSearch.getStatus() == BMAP_STATUS_SUCCESS && results.getCurrentNumPois() >= 1){
            var data = results.getPoi(0);
            document.getElementById("<%=TextBoxJingDu.ClientID %>").value = data.point.lng;
            document.getElementById("<%=TextBoxWeiDu.ClientID %>").value =  data.point.lat;
            map.centerAndZoom(data.point, map.getZoom());
            marker.setPosition(data.point);
        }else {
        alert('找不到相应的位置！');
        }
      }
    });

    

//    var autocomplate = new BMap.Autocomplete(    //建立一个自动完成的对象
//    {"input" : "suggestId"
//        ,"location" : map
//    });
//    
//    autocomplate.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
//    var str = "";
//        var _value = e.fromitem.value;
//        var value = "";
//        if (e.fromitem.index > -1) {
//            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
//        }    
//        str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
//    
//        value = "";
//        if (e.toitem.index > -1) {
//            _value = e.toitem.value;
//            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
//        }    
//        str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
//    });

//    var myValue;
//    autocomplate.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
//    var _value = e.item.value;
//        myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
//        setPlace();
//    });

//    function setPlace(){
//        //map.clearOverlays();    //清除地图上所有覆盖物
//        function myFun(){
//            var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
//            map.centerAndZoom(pp, map.getZoom());
//            marker.setPosition(pp);
//            document.getElementById("<%=TextBoxJingDu.ClientID %>").value = pp.lng;
//            document.getElementById("<%=TextBoxWeiDu.ClientID %>").value = pp.lat;
//        }
//        var local = new BMap.LocalSearch(map, { //智能搜索
//          onSearchComplete: myFun
//        });
//        local.search(myValue);
//    }
    <%} %>

</script>